from django.shortcuts import render
from django.http import HttpResponse
from app1 import models
# Create your views here.
import uuid
import time
import django.utils.timezone as timezone


'''
CRUD

'''
def create(request):
	print(timezone.now)
	models.UserInfo.objects.create(
		KeyId = uuid.uuid1(),
		Name = "张三1",
		Age = 18,
		CreateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
		Version = 0,
		IsDelete = 0
	)
	
	obj = models.UserInfo(
		KeyId = uuid.uuid1(),
		Name = "李四",
		Age = 18,
		CreateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
		Version = 0,
		IsDelete = 0
	)
	obj.save()
	dic = {
		'KeyId' : uuid.uuid1(),
		'Name' : "王五",
		'Age' : 18,
		'CreateTime' : time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
		'Version' : 0,
		'IsDelete' : 0
	}
	models.UserInfo.objects.create(**dic)
	return HttpResponse('OK');

def find(request):
	all = models.UserInfo.objects.all();
	print(all)
	print(all[0])
	print("{:-^30}".format('-'))
	list1 = models.UserInfo.objects.filter(Name="李四")
	print(list1)
	for x in list1:
		print(x.KeyId + " " + x.Name + " ")

	return HttpResponse('OK')

def delete(request):
	obj = models.UserInfo.objects.filter(Name="李四").delete()
	print(obj)
	return HttpResponse('OK')

def index(request):
	userall = models.UserInfo.objects.all()
	#(5, {'app1.UserInfo': 5})
	print(userall)
	

	return render(request, 'index.html', {
		'userall': userall
	});

def update(request):
	userlist = models.UserInfo.objects.filter(CreateTime="2017-10-10 12:12:00").update(Name='asdfsdf')
	print(userlist);
	return HttpResponse('OK')



